package com.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gym.pojo.Course;
import com.gym.pojo.CourseSort;
import com.gym.service.CourseService;
import com.gym.service.CourseSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourseController {

    @Autowired(required = false)
    CourseService courseService;

    @Resource
    CourseSortService courseSortService;


    @RequestMapping("/toCourse")
    @ResponseBody
    public Map<String,Object> toCourse(Integer page,Integer limit,String courseName){

        Page<Course> courses = new Page<>(page,limit);
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        //根据 课程名 模糊查询
        queryWrapper.like(courseName != null,Course::getCourseName,courseName);
        Page<Course> coursePage = courseService.page(courses,queryWrapper);
        List<Course> courseList = coursePage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("data",courseList);
        map.put("count",coursePage.getTotal());
        return map;
    }

    @RequestMapping("/addCourse")
    @ResponseBody
    public Map<String,Object> addCourse(Course course) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = courseService.save(course);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    @RequestMapping("/delCourse")
    @ResponseBody
    public Map<String,Object> delCourse(String[] courseId) {

        HashMap<String, Object> map = new HashMap<>();
        //批量删除
        boolean save = courseService.removeByIds(Arrays.asList(courseId));
        if (save){
            map.put("save",save);
            map.put("data","删除成功");
        }else {
            map.put("save",save);
            map.put("data","删除成功");
        }
        return map;
    }

    @RequestMapping("/updateCourse")
    @ResponseBody
    public Map<String,Object> updateCourse(Course course) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = courseService.updateById(course);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }


    //课程分类
    @RequestMapping("/toCourseSort")
    @ResponseBody
    public Map<String,Object> toCourseSort(Integer page,Integer limit,String coursesortName){

        Page<CourseSort> courseSorts = new Page<>(page,limit);
        LambdaQueryWrapper<CourseSort> queryWrapper = new LambdaQueryWrapper<>();
        //根据 课程分类名 模糊查询
        queryWrapper.like(coursesortName != null,CourseSort::getCoursesortName,coursesortName);
        Page<CourseSort> courseSortPage = courseSortService.page(courseSorts,queryWrapper);
        List<CourseSort> courseSortList = courseSortPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("data",courseSortList);
        map.put("count",courseSortPage.getTotal());
        return map;

    }

    @RequestMapping("/addCourseSort")
    @ResponseBody
    public Map<String,Object> addCourseSort(CourseSort courseSort) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = courseSortService.save(courseSort);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    @RequestMapping("/delCourseSort")
    @ResponseBody
    public Map<String,Object> delCourseSort(String[] coursesortId) {
        HashMap<String, Object> map = new HashMap<>();
        //批量删除
        boolean save = courseSortService.removeByIds(Arrays.asList(coursesortId));
        if (save){
            map.put("save",save);
            map.put("data","删除成功");
        }else {
            map.put("save",save);
            map.put("data","删除成功");
        }
        return map;
    }

    @RequestMapping("/updateCourseSort")
    @ResponseBody
    public Map<String,Object> updateCourseSort(CourseSort courseSort) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = courseSortService.updateById(courseSort);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }

}